package com.lzh.others.leetcode;

import java.util.Arrays;

/**
 * 官方答案
 */
public class LeetCode1371_guanfang {
    static class Solution {
        // int[32]
        // 0 0 0 0 0

        public int findTheLongestSubstring(String s) {
            int n = s.length();
            //创建一个长度位32的数组，二进制的首表示索引值，后面的五位分别表示元音字母在这个索引位置时的奇偶性，偶数就为0，奇数就为1
            int[] pos = new int[1 << 5];//<<是左位移操作符，对于任何整数n,表达式1<<n表示将二进制的1向左移动n位,这里是移动5位 000001 > 0100000
            Arrays.fill(pos, -1);
            int ans = 0, status = 0;//当前奇偶状态
            pos[0] = 0;
            for (int i = 0; i < n; i++) {
                char ch = s.charAt(i);
                if (ch == 'a') {
                    status ^= (1 << 0);
                    //^= 异或运算， 都为1则为0，都为0则为0，一个1一个0则为1 牛啊 我想不到还能这么实现，一次遍历就实现了 太秀了
                    //翻译: 每一次循环匹配时改变这个元音字符出现次数的奇偶状态，比如这里如果本次取出的字母为a则出现次数+1;即:改变它的奇偶状态 0->1 或 1->0
                } else if (ch == 'e') {
                    status ^= (1 << 1);
                } else if (ch == 'i') {
                    status ^= (1 << 2);
                } else if (ch == 'o') {
                    status ^= (1 << 3);
                } else if (ch == 'u') {
                    status ^= (1 << 4);
                }
                if (pos[status] >= 0) {
                    ans = Math.max(ans, i + 1 - pos[status]);
                } else {
                    pos[status] = i + 1;
                }
            }
            return ans;
        }
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        Solution solution = new Solution();
        solution.findTheLongestSubstring("lcwbgnkxnooovtxtuuolbgvieqyxdzyngkdaxuhgwzmgjjvylhavhtijfzvwdalxewsympoyvdhtlbchfgnexmmslcbrozsljwlxzkladmjibvyqpibpkwnxosrqhfqbmigkgxdtfnnsitxecihwisfonaejqeenboqxpqxbmdwzxjnodurnznaitbjikqaerbgkxdfjxnqyecqpqcnluezomtkvjprcqeaucgttwlpqeyfwenybcluxwzjvixlljnmpolktwnezewpiuwinsptbjqzplpeoeuwpehkycvrsslfznunjihtauplcipxmobnipqekegxmddzkepuqzoqepebgwmkuxipjbncgaskmzjhjnlokvxidvlbjgxsdtxiaikdmojrilepbdmokaouhekegpfjlhuywvgtbtozivvooplnnchlbkdvmrwwpkegczippokorcptixrudwszencxemdnxrgearvtddcukdvsrulnvmcnuojxynomtrteotpmdkiueivbdyfxvbakkbfnotanrsmdvmbaehqpumkejtessereyeshxgagprozlpunqanhwmitayqkdzqyhnieqosyrlidxxxcwlmolnvqopmrabpkyhdalcwfwpdheoxcfuiypwlfhbjuorrotjhajpeggfjcifmuzyszjxyfomvczltlhazrtfotejmekkiaegiguabubojhqyudxctegfanffokbotqlwjsertqbhdophqkdvjeiunidaalqfbkfbbaihaxjpxxtupkscgflwxczlelgomoltschhdhcxjqlcdstlhrjyujzdfwngoeygcncmeshjitqxspmddvlopvmgskeuikugtbvmikzdueglpuksbbwoeopqygziitejdkhkkgydngnyeqrcmamemltyqmtexvcyvqhygppmywkiraqyqwxxiamxqmeftidndwpmbpcschkmtvvklwvyihneyhvmtxbhgseskfdygnbqkrywllvwnhlymchoelulcoqznmyicooveiwwazqlnhlwozkoqyaezwyhzwdhilhsfollwbwvfmrllvrsnjqiocbdrixjzgnrszghicnmvqjjtpcnhwgpdqlsyrcbckhocgboecvpczbepewpnpjatirudtcurrtlbhrvreaymbekvmrzctyojkgnpcmyizauvudjxhssmamcuschcnqorjqybepptlerdgirzapuvsoeekmoitoszlwmyvtscagrhhtvlnrydcayhukhxgqfrcqfbjuiswvklnlygatspefmafailygnvgzhnvsgnsmbfsaugdbjermtlfsyvffqepucofzyjilohswbxhugzzihzxwnxwiwhlzpnzgoqvqzkbhhrendrcynnqmtodzgbmgrwgnqxqwtqtpyludmuvyjqzlkfnvzrhgchcivnxhpuibmlmongbjheekvvxrvjtsonvjlxkihomxkbsygzyvdvuehlewqoymiankekcgmgmtqrcvuogdezmdmyqsxzyhoyprcfyalbsoeypmgxgtbkgpixfrpqxqnbegajnwcfuamjqejefjqhzzvjzotgqgwbgtmklvmybvmcfpeljfbwpollltqxrgyrehnzvchijrokuoapwpeanmfjrwmgsvmzxwahupklojcbqkfeblphtuwbbapooynhmfdhjjhadgnvjpyizjduoeikzjsrkbjgioqxqhyakodcnlibewwuqjlzkmfybokfthpftnrmdjyctofsnpuexcarwrpstrehbrzjnzvsoislrkqlsbfnhpulvixxyhctijqybjppwfcvsqrgbssqkjcrthsufevxnutkuwgtyovfaygbacmzlngfanbhbmdllrtemqudxkpuaohmrcntsmalqjhmpmlbbntnsfwekgepnkyyalllxczymewubebxjikbceehkgyfognlejzsfoasfxudbxenvjqygbctqyqrwnxiwprooustcskdjhybhdgfuymymueshrugpwsuevxuydmolvatzpupybhtjfqtpcxkciwzqjverubkceqwefvyatrabcyrlpbvquvrcoosvailoclcffysasmgsvhepdlqvptdxtrvfqjdjctdskrosxsjojyqozzrosejsicvjbsmwepsuyxwkvidsjnhhmrscmveibviyhkcimdehpkonycqsobngmssvyittyeembauboalqokzfwvgxotgvgmkywoiifjvcyttrcqtpssrhdpdoqrfdwukqwzmshqdzohdycpisqjaktzvzmhneqsocpinxzrsrcwdmdimbcrlcmvppxvuufmbimvmmoawvhrecpmgdywwogmumnaziaybpxneytfxgqmjcrbnweofjbujsjbtqkjbpcfxuyvytmwupiozanbjyblgkfhecbdejkpsstpamsrmhxqerydnrmhratsrknzmruvgenfdsdxjflppyqfsiuwhicwqkhuztrhiyfuebolethjxbnopxbfrmzvvpuxkjiflvqxxlcynhwldlhwclvrvvvjbslqamvdaasfodglcqemfotdqiwpzflveduecumffhzjdbtgatwgazuowgtmjekltmdzttgkanoxgfaqiohzccgtxehzhwrlqqasqgobdfnevzpqhhblfzrvfnrhilyyuztrgjshdrbkjrfxenkrjtayurhakpbgmdowuzmprrgyyodvugyyufuitwiaczvvmazgqkiusgypreobmqmmbubjlhxgorppbyrgdkwjnxzhwxfisivrsldvwlnqnophdfqtotobdcmmyiguvpsvvltmrqzdsahpmiticfmdzb");
        System.out.println("cost = " + (System.currentTimeMillis() - l) + "ms");
    }

}
